import { readdir, opendir, readFile } from 'fs';
const file1 = '../demo1';
const file2 = './files/app021.txt';

//读取目录
readdir(file1, (err, list) => {
    if(err) {
    }else {
        console.log('readdir', list);//[ 'app01.js', 'app02.js', 'package.json' ]
    }
});
//打开目录
opendir(file1, (err, list) => {
    if(err) {
    }else {
        console.log('opendir');
        //console.log('opendir', list);
        /*
            Dir {
                [Symbol(kDirHandle)]: DirHandle {},
                [Symbol(kDirBufferedEntries)]: [],
            }
        */
    }
});

//读取文件
readFile(file2, (err, data) => {
    if(err) {
    }else {
        console.log('readFile', data.toString());//readFile abc
    }
});

/*
-----------------------------------------------
*/


import { open, close, read } from 'fs';
//读取内容 1
open('./files/app022.txt', (err, fd) => {
    if(err) {
    }else {
        var buf = Buffer.alloc(12);
        read(fd, buf, 0, 12, null, function(err,bytesRead, buffer){
            console.log('read 1------------------');
            if(err){
            }else {
                console.log('len:' + bytesRead);//len:3
                console.log(buffer.toString());//abc
                console.log(buf.toString());//abc
            }
            console.log('read 1------------------');
            //打开的文件需要关闭
            close(fd, function () {});
        });
    }
});
//读取内容 2
open('./files/app023.txt', (err, fd) => {
    if(err) {
    }else {
        read(fd, (err, bytesRead, bbuffer) => {
            console.log('read 2------------------');
            console.log('bytesRead:' + bytesRead);//bytesRead:3
            console.log(bbuffer.toString());//abc
            console.log('read 2------------------');
            //打开的文件需要关闭
            close(fd, function () {});
        });
    }
});